﻿using App.Models;
using App.TestApi.Controllers.Lifes;
using App.TestApi.Controllers.Todos.Dto;
using App.TestApi.Models;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Serilog.Context;
using System.Text;

namespace App.TestApi.Controllers {
    public class TodoController : AppController {

        private readonly Iscope _scope;
        private readonly Isingleton _singleton;
        private readonly Itransient _transient;
        private readonly ILogger<TodoController> _logger;
        private readonly AppTestApiDbContext _db;

        public TodoController(Iscope scope, Isingleton singleton, Itransient transient,
            IMapper mapper, ILogger<TodoController> logger,
            AppTestApiDbContext db) : base(mapper)
        {
            _scope = scope;
            _singleton = singleton;
            _transient = transient;
            _logger = logger;
            _db = db;
        }

        [HttpGet]
        [ApiVersionCustom(AppApiVersion.V1)]
        public AppResponse<TodoDto> Get()
        {
            _logger.LogInformation("get 测试");

            using (LogContext.PushProperty("mesId", 1111))
            {
                _logger.LogError("增加字段列");
            }

            var info = new Todo();
            info.id = 1;
            info.Title = "v1";
            info.IsComplete = false;

            var resultDto = _mapper.Map<TodoDto>(info);
            _logger.LogWarning("数据库插入测试warning");
            _logger.LogError("数据库插入测试error");
            return resultDto;
        }

        [HttpGet]
        public AppResponse<string> ping()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("瞬时模式：");
            stringBuilder.Append(_transient.Get());
            stringBuilder.Append("；单例模式：");
            stringBuilder.Append(_singleton.Get());
            stringBuilder.Append("；同个生命周期模式：");
            stringBuilder.Append(_scope.Get());

            return stringBuilder.ToString();
        }

        [HttpGet]
        public AppResponse<string> ping1()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("同个生命周期模式：");
            stringBuilder.Append(_scope.Get());
            stringBuilder.Append("；2：");
            stringBuilder.Append(_scope.Get());

            return stringBuilder.ToString();
        }

        [HttpGet]
        public AppResponse<bool> AddAndUpdate(int? id = null, string? title = "")
        {
            if (id.HasValue)
            {
                var todo = _db.Todo.Where(p => p.id == id).FirstOrDefault();
                if (todo == null)
                {
                    return AppResponse<bool>.FailResult("未找到备忘录");
                }
                todo.Title = title;
            }
            else
            {
                var todo = new Todo();
                todo.Title = "测试1";
                todo.IsComplete = false;

                _db.Add(todo);
            }

            var row = _db.SaveChanges();
            return row > 0;
        }

        [HttpGet]
        public AppResponse<Todo> GetById(int id)
        {
            var todo = _db.Todo.Where(p => p.id == id).FirstOrDefault();

            return todo;
        }

        [HttpGet]
        public AppResponse<bool> Remove(int id)
        {
            var todo = _db.Todo.Where(p => p.id == id).FirstOrDefault();
            if (todo == null)
            {
                return AppResponse<bool>.FailResult("未找到备忘录");
            }

            _db.Remove(todo);
            var row = _db.SaveChanges();
            return row > 0;
        }

    }
}
